package com.dhwl.hsh.merchant.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.dhwl.hsh.lib.core.HshException;
import com.dhwl.hsh.lib.core.mvp.MvpView;
import com.dhwl.hsh.lib.utils.StringUtils;
import com.dhwl.hsh.lib.utils.SystemTool;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.common.Constant;
import com.dhwl.hsh.merchant.presenter.FixPwdPresenter;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 修改密码
 *
 * @author Derek.Kidd
 * @time 2016/5/4 18:25
 */
public class FixLoginPwdActivity extends BaseActivity implements MvpView {

    @Bind(R.id.edit_old_pwd)
    EditText editOldPwd;
    @Bind(R.id.edit_new_pwd)
    EditText editNewPwd;
    @Bind(R.id.edit_new_pwd_again)
    EditText editNewPwdAgain;
    @Bind(R.id.btn_sure)
    Button btnSure;

    private String oldPwd;

    private String firstNewPwd;

    private String secondNewPwd;

    FixPwdPresenter fixPwdPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fix_login_pwd;
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setTitleContent(R.string.fix_pwd);

    }

    @Override
    protected void initData() {
        fixPwdPresenter = new FixPwdPresenter();
        fixPwdPresenter.attachView(this);

    }

    @OnClick(R.id.btn_sure)
    public void onClick() {

        // 获取密码、验证
        oldPwd = editOldPwd.getText().toString().trim();
        // 密码
        if (StringUtils.isEmpty(oldPwd)) {
            showToast(R.string.user_login_validate_pwd_is_null);
            return;
        }
        // 密码长度验证
        if (oldPwd.length() < Constant.Rule.PWD_MIN_LENGTH || oldPwd.length() > Constant.Rule.PWD_MAX_LENGTH) {
            showToast(getString(R.string.user_login_pwd_validate_length,
                    Constant.Rule.PWD_MIN_LENGTH,
                    Constant.Rule.PWD_MAX_LENGTH));
            return;
        }
        // 获取字段信息并验证
        firstNewPwd = editNewPwd.getText().toString().trim();
        secondNewPwd = editNewPwdAgain.getText().toString().trim();
        // 验证码为空，返回
        if (StringUtils.isEmpty(firstNewPwd)) {
            showToast(R.string.user_login_validate_pwd_is_null);
            return;
        }
        if (StringUtils.isEmpty(secondNewPwd)) {
            showToast(R.string.user_login_validate_pwd_is_null);
            return;
        }
        // 密码长度验证
        if (firstNewPwd.length() < Constant.Rule.PWD_MIN_LENGTH
                || firstNewPwd.length() > Constant.Rule.PWD_MAX_LENGTH) {
            showToast(getString(R.string.user_login_pwd_validate_length,
                    Constant.Rule.PWD_MIN_LENGTH,
                    Constant.Rule.PWD_MAX_LENGTH));
            return;
        }
        if (secondNewPwd.length() < Constant.Rule.PWD_MIN_LENGTH
                || secondNewPwd.length() > Constant.Rule.PWD_MAX_LENGTH) {
            showToast(getString(R.string.user_login_pwd_validate_length,
                    Constant.Rule.PWD_MIN_LENGTH,
                    Constant.Rule.PWD_MAX_LENGTH));
            return;
        }
        //两次新的密码是否一致
        if (!TextUtils.equals(firstNewPwd, secondNewPwd)) {
            showToast(R.string.toast_confirm_identity_pwd);
            return;
        }
        //新旧密码是否一致
        if (TextUtils.equals(firstNewPwd, oldPwd)) {
            showToast(R.string.toast_confirm_same_pwd);
            return;
        }
        // 密码太简单
        if (StringUtils.equalStr(firstNewPwd) || StringUtils.isOrderNumeric(firstNewPwd)
                || StringUtils.isOrderNumeric_(firstNewPwd)) {
            showToast("密码太简单了,请重新设置");

            return;
        }
        fixPwdPresenter.fixPwd(oldPwd, firstNewPwd);
        SystemTool.hideSysKeyBoard(FixLoginPwdActivity.this);
    }


    @Override
    public void onFailure(HshException e) {

    }

    @Override
    public void onSuccess(Object object) {
        finish();
    }
}
